//
//  CycleSingleLinkedList.swift
//  Swift-LeetCode
//
//  Created by 卢悦明 on 2024/1/25.
//

import UIKit


/// 单向循环链表
struct SingleCycleLinkedList<T: Equatable> {

    var size: Int = 0
    private var firstNode: ListNode<T>?
    
    // 添加节点
    mutating func add(_ index: Int, value: T) {
        guard index >= 0 else {
            return
        }
       if index == size {
           if size == 0 {
               firstNode = ListNode(value, next: firstNode)
           } else {
               let lastNode = nodeOfIndex(size-1)
               let newLast = ListNode(value, next: firstNode)
               lastNode?.next = newLast
               newLast.next = firstNode
           }
        } else  if index == 0 {
            let lastNode = nodeOfIndex(size-1)
            firstNode = ListNode(value, next: firstNode)
            lastNode?.next = firstNode
        } else {
            let perNode = nodeOfIndex(index - 1)
            let newNode = ListNode(value, next: perNode?.next)
            perNode?.next = newNode
        }
        size += 1
    }
    // 删除节点
    @discardableResult mutating func remove(_ index: Int) -> T? {
        guard index >= 0 && index < size else {
            return nil
        }
        var node = firstNode
        if index == 0 {
            if size == 1 {
                firstNode = nil
            } else {
                let lastNode = nodeOfIndex(size-1)
                firstNode = firstNode?.next
                lastNode?.next = firstNode
            }
        } else {
            let per = nodeOfIndex(index-1)
            node = per?.next
            per?.next = node?.next
        }
        size -= 1
        return node?.val
    }
    // 修改节点
    func setNode(_ index: Int, value: T) -> T? {
        let node = nodeOfIndex(index)
        let temp = node?.val
        node?.val = value
        return temp
    }
    // 查询节点
    func getNode(_ index: Int) -> T? {
        let node = nodeOfIndex(index)
        return node?.val
    }
    // 获取某个节点的位置
    func indexOf(_ value: T) -> Int {
        var tempNode = firstNode
        for i in 0..<size {
            if tempNode?.val == value {
                return i
            }
            tempNode = tempNode?.next
        }
        return -1
    }
//    清空数据
    mutating func clear() {
        size = 0
        firstNode = nil
    }
    // 通过索引获取节点
    func nodeOfIndex(_ index: Int) -> ListNode<T>? {
        guard index >= 0 && index < size else {
            print("=============", index)
            return nil
        }
        var temp: ListNode<T>? = firstNode;
        for _ in 0..<index {
            temp = temp?.next
        }
        return temp
    }
    
    
}

extension SingleCycleLinkedList {
    
    // 通过数组创建一个单项链表
    @discardableResult mutating func creatList(array: [T]) -> ListNode<T>? {
        guard array.count != 0 else {
            return nil
        }
        let nodeArray = array.compactMap { ListNode($0) }
        let head = ListNode(0 as! T)
        var temp = head
        for node in nodeArray {
            temp.next = node
            temp = node
        }
        
        size = array.count
        firstNode = head.next
        temp.next = firstNode
        return head.next
    }
    
    // 打印一个单向循环链表
    func printList() {
        guard let firstN = firstNode else {
            print("单向循环链表无节点")
            return
        }
        var head = firstN.next
        var printArray = [String]()
        printArray.append("\(firstN.val)->")
        while let temp = head, head !== firstNode {
            printArray.append("\(temp.val)->")
            head = head?.next ?? firstN
        }
        printArray.append("firstNode:\(firstNode!.val)")
        var printStr = ""
        printArray.forEach {
            printStr = printStr + $0
        }
        print(printStr)
    }
}
